<?php
include_once "xAP-functions/webincludes.php";

$id=intval($_POST["id"]);
$page=intval($_POST["page"]);
$section=substr($_POST["section"],0,20);
$added=array();
$current=array();
//get current list
$result=dbquery("select EventID from xAP_EventLink where EventSourceID=".$id);
foreach($result as $row){
	$current[]=$row["EventID"];
}


// add new events
if (isset($_POST["event"])){
	foreach($_POST["event"] as $value){
		//new event to add
		if(in_array($value,$current)==false){
			dbexec("insert into xAP_EventLink (EventID,EventSourceID,Active) VALUES (".intval($value).",".$id.",1)");
			
		}
		$added[]=intval($value);
	}
}

// delete evetns
foreach($current as $value){
	//delete unknown
	if(!in_array($value,$added)){
		dbexec("delete from xAP_EventLink where EventSourceID=".$id." and EventID=".$value);
	}

}

header("Location:source.php?page=".intval($_GET["page"])."&section=".$section);

?>